﻿namespace AntBlazor.Components;

/// <summary>
/// 分页组件类。
/// </summary>
public partial class AntPagination
{
    private int PageCount => (int)Math.Ceiling(TotalCount * 1.0 / PageSize);

    /// <summary>
    /// 取得或设置总记录数。
    /// </summary>
    [Parameter] public int TotalCount { get; set; }

    /// <summary>
    /// 取得或设置每页大小。
    /// </summary>
    [Parameter] public int PageSize { get; set; }

    /// <summary>
    /// 取得或设置页码。
    /// </summary>
    [Parameter] public int PageIndex { get; set; }

    private string GetUrl(int page)
    {
        if (page <= 0)
            page = 1;
        if (page >= PageCount)
            page = PageCount;

        var url = Navigation.Uri.Split('?')[0];
        var uri = new Uri(Navigation.Uri);
        var queries = System.Web.HttpUtility.ParseQueryString(uri.Query);
        queries["page"] = $"{page}";
        var items = new List<string>();
        foreach (var key in queries.AllKeys)
        {
            items.Add($"{key}={queries[key]}");
        }
        var param = string.Join("&", items);
        return $"{url}?{param}";
    }
}